package com.atguigu.tingshu.payment.strategy.impl;

import com.atguigu.tingshu.account.client.RechargeInfoFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.payment.config.WxPayV3Config;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.atguigu.tingshu.payment.strategy.PaymentStrategy;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.payment.WeChatPayVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Author  王东剑
 * Created on 25/6/2024 PM5:34
 * Package_name com.atguigu.tingshu.payment.strategy.impl
 * Description
 */
@Service
@Slf4j
public class RechargePaymentStrategy implements PaymentStrategy {
	@Autowired
	private RechargeInfoFeignClient rechargeInfoFeignClient;
	@Autowired
	private UserInfoFeignClient userInfoFeignClient;
	@Autowired
	private WxPayV3Config wxPayV3Config;
	@Autowired
	private PaymentInfoService paymentInfoService;
	@Override
	public WeChatPayVo createJsapi(String paymentType, String orderNo) {
		// 根据订单编号查询订单数据
		RechargeInfo rechargeInfo = rechargeInfoFeignClient.getByOrderNo(orderNo).getData();
		// 如果订单已取消不做任何操作
		if (SystemConstant.ORDER_STATUS_CANCEL.equals(rechargeInfo.getRechargeStatus())) return null;
		// 保存交易信息
		paymentInfoService.savePaymentInfo(rechargeInfo);


		log.info("RechargePaymentStrategy.createJsapi.........");
		return this.setParamsAndGetWeChatPayVo(wxPayV3Config, userInfoFeignClient, "充值", orderNo);
	}

	@Override
	public String getPaymentType() {
		return SystemConstant.PAYMENT_TYPE_RECHARGE;
	}
}
